package com.xx.doublexchat.common.user.controller;


import com.xx.doublexchat.common.common.domain.vo.response.ApiResult;
import com.xx.doublexchat.common.common.utils.AssertUtils;
import com.xx.doublexchat.common.common.utils.RequestHolder;
import com.xx.doublexchat.common.user.domain.dto.ItemInfoDTO;
import com.xx.doublexchat.common.user.domain.dto.SummeryInfoDTO;
import com.xx.doublexchat.common.user.domain.enums.RoleEnum;
import com.xx.doublexchat.common.user.domain.vo.request.BlackRequest;
import com.xx.doublexchat.common.user.domain.vo.request.ItemInfoRequest;
import com.xx.doublexchat.common.user.domain.vo.request.ModifyNameRequest;
import com.xx.doublexchat.common.user.domain.vo.request.SummeryInfoRequest;
import com.xx.doublexchat.common.user.domain.vo.request.WearingBadgeRequest;
import com.xx.doublexchat.common.user.domain.vo.response.BadgesResponse;
import com.xx.doublexchat.common.user.domain.vo.response.UserInfoResponse;
import com.xx.doublexchat.common.user.service.IRoleService;
import com.xx.doublexchat.common.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author xiaoXuan
 * @since 2023-12-30
 */
@RestController
@RequestMapping("/capi/user")
@Api(tags = "用户模块接口")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IRoleService roleService;

    @GetMapping("/userInfo")
    @ApiOperation("获取用户个人信息")
    public ApiResult<UserInfoResponse> getUserInfo() {
        return ApiResult.success(userService.getUserInfo(RequestHolder.get().getUid()));
    }

    @PutMapping("/name")
    @ApiOperation("修改用户名")
    public ApiResult<Void> modifyName(@Valid @RequestBody ModifyNameRequest modifyNameRequest) {
        userService.modifyName(RequestHolder.get().getUid(),modifyNameRequest.getName());
        return ApiResult.success();
    }

    @GetMapping("/badges")
    @ApiOperation("可选徽章预览")
    public ApiResult<List<BadgesResponse>> badges() {
        return ApiResult.success(userService.badges(RequestHolder.get().getUid()));
    }

    @PutMapping("/badge")
    @ApiOperation("佩戴徽章")
    public ApiResult<Void> wearingBadges(@Valid @RequestBody WearingBadgeRequest req) {
        userService.wearingBadges(RequestHolder.get().getUid(),req.getItemId());
        return ApiResult.success();
    }

    @PutMapping("/black")
    @ApiOperation("拉黑用户")
    public ApiResult<Void> black(@Valid @RequestBody BlackRequest req) {
        Long uid = RequestHolder.get().getUid();
        boolean hasPower = roleService.hasPower(uid, RoleEnum.ADMIN);
        AssertUtils.isTrue(hasPower,"没有管理员权限");
        userService.black(req);
        return ApiResult.success();
    }

    @PostMapping("/public/summary/userInfo/batch")
    @ApiOperation("用户聚合信息-返回的代表需要刷新的")
    public ApiResult<List<SummeryInfoDTO>> getSummeryUserInfo(@Valid @RequestBody SummeryInfoRequest req) {
        return ApiResult.success(userService.getSummeryUserInfo(req));
    }

    @PostMapping("/public/badges/batch")
    @ApiOperation("徽章聚合信息-返回的代表需要刷新的")
    public ApiResult<List<ItemInfoDTO>> getItemInfo(@Valid @RequestBody ItemInfoRequest req) {
        return ApiResult.success(userService.getItemInfo(req));
    }
}

